
package me.zhengjie.service.impl;

import me.zhengjie.domain.AppWorkCoverPoint;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.modules.system.service.dto.UserDto;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.AppWorkCoverPointRepository;
import me.zhengjie.service.AppWorkCoverPointService;
import me.zhengjie.service.dto.AppWorkCoverPointDto;
import me.zhengjie.service.dto.AppWorkCoverPointQueryCriteria;
import me.zhengjie.service.mapstruct.AppWorkCoverPointMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author IQW
* @date 2022-07-18
**/
@Service
@RequiredArgsConstructor
public class AppWorkCoverPointServiceImpl implements AppWorkCoverPointService {

    private final AppWorkCoverPointRepository appWorkCoverPointRepository;
    private final AppWorkCoverPointMapper appWorkCoverPointMapper;
    private final UserService userService;

    @Override
    public Map<String,Object> queryAll(AppWorkCoverPointQueryCriteria criteria, Pageable pageable){

        //只查看本区域的
        UserDto user = userService.findById(SecurityUtils.getCurrentUserId());
        criteria.setAreaId(user.getAreaId());

        Page<AppWorkCoverPoint> page = appWorkCoverPointRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(appWorkCoverPointMapper::toDto));
    }

    @Override
    public List<AppWorkCoverPointDto> queryAll(AppWorkCoverPointQueryCriteria criteria){
        return appWorkCoverPointMapper.toDto(appWorkCoverPointRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AppWorkCoverPointDto findById(Integer id) {
        AppWorkCoverPoint appWorkCoverPoint = appWorkCoverPointRepository.findById(id).orElseGet(AppWorkCoverPoint::new);
        ValidationUtil.isNull(appWorkCoverPoint.getId(),"AppWorkCoverPoint","id",id);
        return appWorkCoverPointMapper.toDto(appWorkCoverPoint);
    }

    @Override
    public int selSonNum(Integer id,Integer areaId) {
        return appWorkCoverPointRepository.selSonNum(id,areaId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AppWorkCoverPointDto create(AppWorkCoverPoint resources) {

        //设置责任区
        UserDto user = userService.findById(SecurityUtils.getCurrentUserId());
        resources.setAreaId(user.getAreaId());

        return appWorkCoverPointMapper.toDto(appWorkCoverPointRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AppWorkCoverPoint resources) {
        resources.setAreaId(null);
        AppWorkCoverPoint appWorkCoverPoint = appWorkCoverPointRepository.findById(resources.getId()).orElseGet(AppWorkCoverPoint::new);
        ValidationUtil.isNull( appWorkCoverPoint.getId(),"AppWorkCoverPoint","id",resources.getId());
        appWorkCoverPoint.copy(resources);
        appWorkCoverPointRepository.save(appWorkCoverPoint);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            appWorkCoverPointRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<AppWorkCoverPointDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AppWorkCoverPointDto appWorkCoverPoint : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("分类ID", appWorkCoverPoint.getCateId());
            map.put("名称", appWorkCoverPoint.getTitle());
            map.put("编号", appWorkCoverPoint.getCodeno());
            map.put("地址", appWorkCoverPoint.getAddress());
            map.put("经度", appWorkCoverPoint.getLon());
            map.put("纬度", appWorkCoverPoint.getLat());
            map.put("排序（越小越前）", appWorkCoverPoint.getOrdid());
            map.put("状态", appWorkCoverPoint.getStatus());
            map.put("创建日期", appWorkCoverPoint.getCreateTime());
            map.put("更新日期", appWorkCoverPoint.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}